package com.awo.loglibrary;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class LogActivity extends AppCompatActivity {
    private Button button;
    private ListView listView;

    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"
    };

    public LogActivity() throws IOException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        button = (Button) findViewById(R.id.BtnLog);
        listView = (ListView) findViewById(R.id.ListLog);


        try {
            int permission = ActivityCompat.checkSelfPermission(LogActivity.this,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(LogActivity.this, PERMISSIONS_STORAGE, 10);
            }
        } catch (Exception e) {
            Log.e("LogTest", e.toString());
        }

        LogUtil.d("成功","一");
        LogUtil.d("成功","二");
        LogUtil.d("失败","三");
        LogUtil.d("成功","四");
        LogUtil.d("失败","五");
        LogUtil.d("成功","六");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        LogUtil.d("成功","七");
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, R.layout.log_list_item,Txt());
        listView.setAdapter(adapter);
//        LogUtil.d("成功","一");
//        LogUtil.d("成功","二");
//        LogUtil.d("失败","三");
//        LogUtil.d("成功","四");
//        LogUtil.d("失败","五");
//        LogUtil.d("成功","六");
//        LogUtil.d("成功","七");
//        final InputStream is = new ByteArrayInputStream("text......".getBytes());
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                FileOutputStream os = null;
//                try {
//                    //新建一个路径信息
//                    File appDir = new File(Environment.getExternalStorageDirectory(), "/LogTest");
//                    if (!appDir.exists()) {
//                        appDir.mkdir();
//                    }
//                    File file = new File(appDir, "log.txt");
//                    Log.i("LogTest", "file path: " + file.getPath());
//                    os = new FileOutputStream(file);
//                    int len = 0;
//                    byte[] buf = new byte[1024];
//                    while (-1 != (len = is.read(buf))) {
//                        os.write(buf, 0, len);
//                        os.flush();
//                    }
//                } catch (Exception e) {
//                    Log.d("writelog",
//                            "read logcat process failed. message: "
//                                    + e.getMessage());
//                } finally {
//                    if (null != os) {
//                        try {
//                            os.close();
//                            os = null;
//                        } catch (IOException e) {
//                            // Do nothing
//                        }
//                    }
//                }
//            }
//        }).start();
    }

    /**
     * 根据行读取内容
     * @return
     */
    public List<String> Txt() {
        //将读出来的一行行数据使用List存储
        String filePath = Environment.getExternalStorageDirectory() +"/LogTest/log.txt"; //存储地址

        List newList = new ArrayList<String>();
        try {
            File file = new File(filePath);
            int count = 0;//初始化 key值
            if (file.isFile() && file.exists()) {//文件存在
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                while ((lineTxt = br.readLine()) != null) {
                    if (!"".equals(lineTxt)) {
//                        String reds = lineTxt.split(".+")[0];//java 正则表达式
//                        Log.d("reds", reds);
                        newList.add(count, lineTxt);
                        count++;
                    }
                }
                isr.close();
                br.close();
            }else {
                Log.e("tag", "can not find file");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newList;

    }

    public void delFile(View view) {
        delFile(Environment.getExternalStorageDirectory() +"/LogTest/log.txt");
        listView.setAdapter(null);
    }

    //    public boolean d(String Tag, String value) {
//        // 打开文件,写入数据
////        String[] buff = new String[1024];
////
////
////        if (buff[1000] != null) {
////            // 写入文件
////        }
//        final InputStream is = new ByteArrayInputStream((Tag + value).getBytes());
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                FileOutputStream os = null;
//                try {
//                    //新建一个路径信息
//                    File appDir = new File(Environment.getExternalStorageDirectory(), "/LogTest");
//                    if (!appDir.exists()) {
//                        appDir.mkdir();
//                    }
//                    File file = new File(appDir, "log.txt");
//                    os = new FileOutputStream(file);
//                    int len = 0;
//                    byte[] buf = new byte[1024];
//                    while (-1 != (len = is.read(buf))) {
//                        os.write(buf, 0, len);
//                        os.flush();
//                    }
//                } catch (Exception e) {
//                    Log.d("writelog",
//                            "read logcat process failed. message: "
//                                    + e.getMessage());
//                    return;
//                } finally {
//                    if (null != os) {
//                        try {
//                            os.close();
//                            os = null;
//                        } catch (IOException e) {
//                            // Do nothing
//                        }
//                    }
//                }
//            }
//        }).start();
//        //
//        Log.d(Tag, value);
//        return true;
//    }

    /**
     * 删除Log文件
     * @param fileName 文件路径和名称
     */
    public static void delFile(String fileName){
        File file = new File(fileName);
        if(file.isFile()){
            file.delete();
        }
        file.exists();
    }

}
